<template>
  <div class="main">
    <a-form-model class="user-layout-login" @keyup.enter.native="handleSubmit">
      <login-account
        ref="alogin"
        @validateFail="validateFail"
        @success="loginSuccess"
        @fail="requestFailed"
      ></login-account>

      <a-form-model-item>
        <a-checkbox @change="handleRememberMeChange" default-checked>自动登录</a-checkbox>
        <router-link :to="{ name: 'forgetPassword' }" class="forge-password" style="float: right"> 忘记密码 </router-link>
      </a-form-model-item>

      <a-form-item style="margin-top: 24px">
        <a-button
          size="large"
          type="primary"
          htmlType="submit"
          class="login-button"
          :loading="loginBtn"
          @click.stop.prevent="handleSubmit"
          :disabled="loginBtn"
          >确定
        </a-button>
      </a-form-item>
    </a-form-model>
  </div>
</template>

<script>
import { timeFix } from '@/utils/timeFix'
import LoginAccount from './LoginAccount'

export default {
  components: {
    LoginAccount,
  },
  data() {
    return {
      rememberMe: true,
      loginBtn: false,
    }
  },
  created() {
    this.$local.remove("ACCESS_TOKEN")
    this.getRouterData()
    this.rememberMe = true
  },
  methods: {
    handleRememberMeChange(e) {
      this.rememberMe = e.target.checked
    },
    /**跳转到登录页面的参数-账号获取*/
    getRouterData() {
      this.$nextTick(() => {
        let temp = this.$route.params.username || this.$route.query.username || ''
        if (temp) {
          this.$refs.alogin.acceptUsername(temp)
        }
      })
    },

    //登录
    handleSubmit() {
      this.loginBtn = true
      this.$refs.alogin.handleLogin(this.rememberMe)
    },
    // 校验失败
    validateFail() {
      this.loginBtn = false
    },
    
    //登录后台失败
    requestFailed(err) {
      let description = err.message || '账号或密码错误'
      this.$notification['error']({
        message: '登录失败',
        description: description,
        duration: 4,
      })
      //账户密码验证码登录错误后更新验证码
      if ((description.indexOf('密码错误') > 0)||description=="验证码错误") {
        this.$refs.alogin.handleChangeCheckCode()
      }
      this.loginBtn = false
    },
    //登录成功
    loginSuccess() {
      this.$router.push({ name: 'index' }).catch(() => {
        console.log('登录跳转首页出错,这个错误从哪里来的')
      })
      this.$notification.success({
        message: '欢迎',
        description: `${timeFix()}，欢迎回来`,
      })
    },

  },
}
</script>
<style lang="less" scoped>
.user-layout-login {
  label {
    font-size: 14px;
  }
  .getCaptcha {
    display: block;
    width: 100%;
    height: 40px;
  }

  .forge-password {
    font-size: 14px;
  }

  button.login-button {
    padding: 0 15px;
    font-size: 16px;
    height: 40px;
    width: 100%;
  }

  .user-login-other {
    text-align: left;
    margin-top: 24px;
    line-height: 22px;

    .item-icon {
      font-size: 24px;
      color: rgba(0, 0, 0, 0.2);
      margin-left: 16px;
      vertical-align: middle;
      cursor: pointer;
      transition: color 0.3s;

      &:hover {
        color: #1890ff;
      }
    }

    .register {
      float: right;
    }
  }
}
</style>
<style>
.valid-error .ant-select-selection__placeholder {
  color: #f5222d;
}
</style>